<template>
  <el-dialog
      title="添加/修改用户信息"
      :visible.sync="visible"
      :id.sync="id"
      @opened="openDialog"
      :before-close="closeDialog"
      width="480px">
    <el-form label-width="80px">
      <el-form-item label="校园卡号" prop="user.cardNumber" size="small">
        <el-input size="small" v-model="user.cardNumber"></el-input>
      </el-form-item>
      <el-form-item label="用户名" prop="user.username" size="small">
        <el-input size="small" v-model="user.username"></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="user.name" size="small">
        <el-input size="small" v-model="user.name"></el-input>
      </el-form-item>
            <el-form-item label="手机" prop="user.phone" size="small">
        <el-input v-model="user.phone"></el-input>
      </el-form-item>
      <el-form-item label="用户类型" prop="user.userType" size="small">
        <el-select size="small" v-model="user.userType">
          <el-option
              v-for="item in userType"
              :key="item.id"
              :label="item.name"
              :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item align="center" style="margin-left: -80px;">
        <el-button type="primary" size="small" plain @click="handleUpdate" v-if="id!=''">修改设备</el-button>
        <el-button type="primary" size="  small" plain @click="onSubmit" v-else>添加设备</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getUserById, updateUser, createUser} from "@/api/user";

export default {
  name: 'UserListEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      userType:[
        {id: 1, name: '学生'},
        {id: 2, name: '教师'},
        {id: 3, name: '维修人员'},
      ],
      user: {}
    }
  },
  methods: {

    // 添加设备信息
    onSubmit() {
      createUser({
        'cardNumber': this.user.classroomName,
        'createTime': this.user.createTime,
        'email': this.user.email,
        'id': this.user.id,
        'name':this.user.name,
        'password': this.user.password,
        'phone': this.user.phone,
        'userStatus':this.user.userStatus,
        'userType':this.user.userType,
        'username':this.user.username,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 更新设备信息
    handleUpdate() {
      console.log(this.classroom)
      updateUser({
        'cardNumber': this.user.cardNumber,
        'createTime': this.user.createTime,
        'email': this.user.email,
        'id': this.user.id,
        'name':this.user.name,
        'password': this.user.password,
        'phone': this.user.phone,
        'userStatus':this.user.userStatus,
        'userType':this.user.userType,
        'username':this.user.username,
      }).then((response) => {
        if (response.data.code === 200) {
          this.$parent.fetchData()
          this.$alert('提交成功', '提示', {
            confirmButtonText: '确定'
          })

        } else {
          this.$alert('提交失败', '提示', {
            confirmButtonText: '确定'
          })
        }
      })
      this.$emit('update:visible', false)

    },

    // 打开对话框
    openDialog() {
      if (this.id !== '') {
        getUserById(this.id).then((response) => {
          this.user = response.data.data;
          // this.classroom.createTime;
        })
      }
    },

    // 关闭对话框
    closeDialog() {
      this.user = {}
      this.$emit('update:visible', false)
    }
  }
}
</script>

<style scoped>

</style>
